home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP10.ZIP / CHAP10 / SCHMOO / ICLASSF.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  5KB  |  203 lines

  1. /*
  2.  * ICLASSF.CPP
  3.  *
  4.  * Implementation of an IClassFactory interface for Schmoo.
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "schmoo.h"
  17.  
  18.  
  19.  
  20. /*
  21.  * CFigureClassFactory::CFigureClassFactory
  22.  * CFigureClassFactory::~CFigureClassFactory
  23.  *
  24.  * Constructor Parameters:
  25.  *  pFR             LPCSchmooFrame that can create documents.
  26.  */
  27.  
  28. CFigureClassFactory::CFigureClassFactory(LPCSchmooFrame pFR)
  29.     {
  30.     m_cRef=0L;
  31.     m_pFR=pFR;
  32.     m_fCreated=FALSE;
  33.     return;
  34.     }
  35.  
  36.  
  37. CFigureClassFactory::~CFigureClassFactory(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CFigureClassFactory::QueryInterface
  49.  * CFigureClassFactory::AddRef
  50.  * CFigureClassFactory::Release
  51.  */
  52.  
  53. STDMETHODIMP CFigureClassFactory::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  54.     {
  55.     *ppv=NULL;
  56.  
  57.     //Any interface on this object is the object pointer.
  58.     if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  59.         *ppv=(LPVOID)this;
  60.  
  61.     /*
  62.      * If we actually assign an interface to ppv we need to AddRef it
  63.      * since we're returning a new pointer.
  64.      */
  65.     if (NULL!=*ppv)
  66.         {
  67.         ((LPUNKNOWN)*ppv)->AddRef();
  68.         return NOERROR;
  69.         }
  70.  
  71.     return ResultFromScode(E_NOINTERFACE);
  72.     }
  73.  
  74.  
  75. STDMETHODIMP_(ULONG) CFigureClassFactory::AddRef(void)
  76.     {
  77.     return ++m_cRef;
  78.     }
  79.  
  80.  
  81. STDMETHODIMP_(ULONG) CFigureClassFactory::Release(void)
  82.     {
  83.     ULONG           cRefT;
  84.  
  85.     cRefT=--m_cRef;
  86.  
  87.     if (0L==m_cRef)
  88.         delete this;
  89.  
  90.     return cRefT;
  91.     }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /*
  100.  * CFigureClassFactory::CreateInstance
  101.  *
  102.  * Purpose:
  103.  *  Instantiates a Figure object that supports embedding.
  104.  *
  105.  * Parameters:
  106.  *  punkOuter       LPUNKNOWN to the controlling IUnknown if we are
  107.  *                  being used in an aggregation.
  108.  *  riid            REFIID identifying the interface the caller desires
  109.  *                  to have for the new object.
  110.  *  ppvObj          LPVOID FAR * in which to store the desired interface
  111.  *                  pointer for the new object.
  112.  *
  113.  * Return Value:
  114.  *  HRESULT         NOERROR if successful, otherwise contains E_NOINTERFACE
  115.  *                  if we cannot support the requested interface.
  116.  */
  117.  
  118. STDMETHODIMP CFigureClassFactory::CreateInstance(LPUNKNOWN punkOuter
  119.     , REFIID riid, LPVOID FAR *ppvObj)
  120.     {
  121.     LPCSchmooDoc        pDoc;
  122.     HRESULT             hr;
  123.  
  124.     *ppvObj=NULL;
  125.  
  126.     //Great idea to protect yourself from multiple creates here.
  127.     if (m_fCreated)
  128.         return ResultFromScode(E_UNEXPECTED);
  129.  
  130.     m_fCreated=TRUE;
  131.     hr=ResultFromScode(E_OUTOFMEMORY);
  132.  
  133.     //We don't support aggregation
  134.     if (NULL!=punkOuter)
  135.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  136.  
  137.     //Try creating a new document, which creates the object.
  138.     pDoc=(LPCSchmooDoc)m_pFR->m_pCL->NewDocument(TRUE, m_pFR->m_pAdv);
  139.  
  140.     if (NULL==pDoc)
  141.         {
  142.         //This will cause shutdown as the object count will go to zero.
  143.         g_cObj++;
  144.         ObjectDestroyed();
  145.         return hr;
  146.         }
  147.  
  148.     //Insure the document is untitled, then get the requested interface.
  149.     pDoc->ULoad(TRUE, NULL);
  150.     pDoc->m_pFigure->FrameSet(m_pFR);
  151.     hr=pDoc->m_pFigure->QueryInterface(riid, ppvObj);
  152.  
  153.     //Closing the document will destroy the object and cause shutdown.
  154.     if (FAILED(hr))
  155.         {
  156.         m_pFR->m_pCL->CloseDocument(pDoc);
  157.         return hr;
  158.         }
  159.  
  160.     return NOERROR;
  161.     }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169.  * CFigureClassFactory::LockServer
  170.  *
  171.  * Purpose:
  172.  *  Increments or decrements the lock count of the serving IClassFactory
  173.  *  object.  When the number of locks goes to zero and the number of
  174.  *  objects is zero, we shut down the application.
  175.  *
  176.  * Parameters:
  177.  *  fLock           BOOL specifying whether to increment or decrement the
  178.  *                  lock count.
  179.  *
  180.  * Return Value:
  181.  *  HRESULT         NOERROR always.
  182.  */
  183.  
  184. STDMETHODIMP CFigureClassFactory::LockServer(BOOL fLock)
  185.     {
  186.     if (fLock)
  187.         g_cLock++;
  188.     else
  189.         {
  190.         g_cLock--;
  191.  
  192.         /*
  193.          * To centralize shutdown, we'll artificially increase the object
  194.          * count here and call ObjectDestroyed, which will decrement the
  195.          * count, see that there are no objects or locks, then shut down.
  196.          */
  197.         g_cObj++;
  198.         ObjectDestroyed();
  199.         }
  200.  
  201.     return NOERROR;
  202.     }
  203.